package com.umeox.watch.moto.launcher.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.widget.ImageView;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.adapter.WatchFaceAdapter;
import com.umeox.watch.moto.launcher.bean.WatchFaceInfo;
import com.umeox.moto.watch.themes.ItemClickListener;
import com.umeox.watch.moto.launcher.event.WatchFaceChangeEvent;
import com.umeox.watch.moto.launcher.ui.help.GalleryLayoutManager;
import com.umeox.watch.moto.launcher.ui.help.ScaleTransformer;
import com.umeox.watch.moto.pedometer.util.CacheImpl;
import com.umeox.watch.moto.pedometer.util.ICache;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


public class WatchFaceActivity extends ThemeAppCompatActivity implements ItemClickListener {
    private RecyclerView mWatchFaceRv;
    private List<WatchFaceInfo> mDataList;
    private WatchFaceAdapter mAdapter;

    private ImageView mStyleSettingIv;

    private final int[] WATCH_FACE_0_ARR = {R.drawable.wallpaper_effect_0_1, R.drawable.wallpaper_effect_0};
    private final int[] WATCH_FACE_1_ARR = {R.drawable.wallpaper_effect_1_1, R.drawable.wallpaper_effect_1};
    private final int[] WATCH_FACE_2_ARR = {R.drawable.wallpaper_effect_2_1, R.drawable.wallpaper_effect_2};
    private final int[] WATCH_FACE_3_ARR = {R.drawable.wallpaper_effect_3_1, R.drawable.wallpaper_effect_3};
    private final int[] WATCH_FACE_4_ARR = {R.drawable.wallpaper_effect_4_1, R.drawable.wallpaper_effect_4};
    private int mCurrentPosition;

    private int currentIconIndex;
    private final int REQUEST_CHOOSE_STYLE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallpaper);

        mWatchFaceRv = findViewById(R.id.rv);
        mStyleSettingIv = findViewById(R.id.iv_style_setting);

        int index = DataProvider.getWallpaper();
        currentIconIndex = CacheImpl.getInstance().get(ICache.KEY_WATCH_FACE_ICON_INDEX, 0);
        Logger.e("index=" + index);
        mDataList = new ArrayList<>();

        initWatchFaceData();

        mDataList.get(index).setCurrentWatchFace(true);
        mDataList.get(index).setIconIndex(currentIconIndex);
        GalleryLayoutManager layoutManager = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
        layoutManager.setItemTransformer(new ScaleTransformer());
        layoutManager.attach(mWatchFaceRv, index);
        mWatchFaceRv.setHasFixedSize(true);
        mAdapter = new WatchFaceAdapter(mDataList, this, R.layout.item_wallpaper);
        mWatchFaceRv.setAdapter(mAdapter);

        layoutManager.setOnItemSelectedListener((recyclerView, item, position) -> {
            mCurrentPosition = position;
            currentIconIndex = mDataList.get(position).getIconIndex();
            mStyleSettingIv.setVisibility(mDataList.get(position).getIconResArr().length > 1 ? View.VISIBLE : View.GONE);
        });

        mStyleSettingIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WatchFaceActivity.this, WatchFaceStyleActivity.class);
                intent.putExtra("extra_watch_res", mDataList.get(mCurrentPosition).getIconResArr());
                intent.putExtra("extra_watch_index", currentIconIndex);
                startActivityForResult(intent, REQUEST_CHOOSE_STYLE);
            }
        });

        mWatchFaceRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) {
                    mStyleSettingIv.setClickable(false);
                } else {
                    mStyleSettingIv.setClickable(true);
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });


    }

    private void initWatchFaceData() {
        final String[] themeIcons = getResources().getStringArray(R.array.wallpaper_effects);
        for (int i = 0; i < themeIcons.length; i++) {
            final int themeIconId = getResources().getIdentifier(themeIcons[i], "drawable", getPackageName());
            WatchFaceInfo watchFaceInfo = new WatchFaceInfo();
            watchFaceInfo.setIconIndex(0);
            watchFaceInfo.setCurrentWatchFace(false);
            if (i == 0) {
                watchFaceInfo.setIconResArr(WATCH_FACE_0_ARR);
            } else if (i == 1) {
                watchFaceInfo.setIconResArr(WATCH_FACE_1_ARR);
            } else if (i == 2) {
                watchFaceInfo.setIconResArr(WATCH_FACE_2_ARR);
            } else if (i == 3) {
                watchFaceInfo.setIconResArr(WATCH_FACE_3_ARR);
            } else if (i == 4) {
                watchFaceInfo.setIconResArr(WATCH_FACE_4_ARR);
            } else {
                int[] arr = new int[]{themeIconId};
                watchFaceInfo.setIconResArr(arr);
            }
            mDataList.add(watchFaceInfo);

        }
    }


    @Override
    public void onItemClicked(int position) {
        DataProvider.setCustomWallpaper("");
        DataProvider.setWallpaper(position);
        CacheImpl.getInstance().put(ICache.KEY_WATCH_FACE_ICON_INDEX, currentIconIndex);
        EventBus.getDefault().post(new WatchFaceChangeEvent(position));
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CHOOSE_STYLE && resultCode == RESULT_OK && data != null) {
            int chooseStyle = data.getIntExtra("current_style", 0);
            Logger.e("chooseStyle=" + chooseStyle);
            if (chooseStyle != currentIconIndex) {
                //更新列表
                currentIconIndex = chooseStyle;
//                mDataList.get(mCurrentPosition).setIconIndex(currentIconIndex);
                mAdapter.notifyDataChanged(mCurrentPosition, currentIconIndex);
            }
        }
    }
}
